package com.laurent.brewerhistory.sql;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DbAdapter extends SQLiteOpenHelper {

	/* CONSTANTES */
	private final static String DATABASE_NAME = "BrewerHistory";
	private final static int DATABASE_VERSION = 1;

	public final static String TABLE_BEER = "beer";
	public final static String TABLE_BREWING = "brewing";
	public final static String TABLE_RECIPE = "recipe";

	private final static String CREATE_BEER = "CREATE TABLE beer (id INTEGER PRIMARY KEY AUTOINCREMENT, color VARCHAR(64), description TEXT, imageUrl VARCHAR(256), name VARCHAR(128));";
	private final static String CREATE_BREWING = "CREATE TABLE brewing (id INTEGER PRIMARY KEY AUTOINCREMENT, startDate DATETIME, endDate  DATETIME, firstFermentation DATETIME, review INTEGER, idRecipe INTEGER);";
	private final static String CREATE_RECIPE = "CREATE TABLE recipe (id INTEGER PRIMARY KEY AUTOINCREMENT, idBeer INTEGER, degree FLOAT, firstFermentation INTEGER, firstSugar INTEGER, firstWater INTEGER, quantity INTEGER, secondFermentation INTEGER, secondSugar INTEGER, secondWater INTEGER);";

	/**
	 * Constructeur par défaut de la super class
	 * 
	 * @param context L'activity courante
	 */
	public DbAdapter(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// Creation de la base de données avec des if Exists évidemment
		db.execSQL(CREATE_BEER);
		db.execSQL(CREATE_BREWING);
		db.execSQL(CREATE_RECIPE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS beer");
		db.execSQL("DROP TABLE IF EXISTS brewing");
		db.execSQL("DROP TABLE IF EXISTS recipe");
		db.execSQL(CREATE_BEER);
		db.execSQL(CREATE_BREWING);
		db.execSQL(CREATE_RECIPE);
	}

}
